package de.java.swing.extension.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;

import de.java.swing.extension.controller.ExtendedMouseAdapter;
import de.java.swing.extension.utils.MenubarConstants;

/**
 * TODO Kommentar.
 * 
 * @author Patrick Haas
 * @since 1.0.0
 * 
 */
public class Menu extends ExtendedPanel implements MenubarConstants {

	private static final long serialVersionUID = 4069274167361394029L;

	private ExtendedPanel northPane;

	private ExtendedPanel northCenterPane;

	private ExtendedPanel northWestPane;

	private ExtendedPanel northEastPane;

	private ExtendedPanel westPane;

	private ExtendedPanel eastPane;

	private ExtendedPanel centerPane;

	private JLabel textLabel;

	private List<MenuItem> menuItems;

	private JDialog popupMenu;

	private int maximumWidth;

	private Color mouseOverColor = DEFAULT_MOUSE_OVER_COLOR;

	private Color mouseOutColor = DEFAULT_MOUSE_OUT_COLOR;

	private int itemHeight = DEFAULT_ITEM_HEIGHT;

	private Menu() {
		initialize();
	}

	public Menu(String title) {
		this();
		this.textLabel.setText(title);
	}

	public void addItem(MenuItem item) {
		item.setMenu(this);
		menuItems.add(item);
		if (maximumWidth < item.getPreferredSize().width) {
			maximumWidth = item.getPreferredSize().width;
		}
	}

	public boolean hasVisibleIcons() {
		for (MenuItem item : menuItems) {
			if (item.isVisible()) {
				return true;
			}
		}

		return false;
	}

	public void hideMenu() {
		popupMenu.setVisible(false);
	}

	public void setMouseOverColor(Color c) {
		mouseOverColor = c;
	}

	public Color getMouseOverColor() {
		return mouseOverColor;
	}

	public void setMouseOutColor(Color c) {
		mouseOutColor = c;
		this.textLabel.setForeground(c);
	}

	public Color getMouseOutColor() {
		return mouseOutColor;
	}

	public void setItemHeight(int height) {
		itemHeight = height;
	}

	public int getItemHeight() {
		return itemHeight;
	}

	private void initialize() {
		this.setLayout(new BorderLayout());

		this.addMouseListener(new ExtendedMouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				textLabel.setForeground(mouseOverColor);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				if (!popupMenu.isVisible()) {
					textLabel.setForeground(mouseOutColor);
				}
			}

			@Override
			public void execute() {
				Menu menu = (Menu) getEvent().getSource();
				Point pos = new Point();
				pos.x = menu.getLocationOnScreen().x;
				pos.y = menu.getLocationOnScreen().y + DEFAULT_MENUBAR_HEIGHT;

				showMenu(pos);
			}
		});

		menuItems = new ArrayList<MenuItem>();

		northPane = new ExtendedPanel();
		northPane.setLayout(new BorderLayout());
		this.add(BorderLayout.NORTH, northPane);

		northWestPane = new ExtendedPanel(1.0f);
		northWestPane.setPreferredSize(new Dimension(4, 4));
		northPane.add(BorderLayout.WEST, northWestPane);

		northCenterPane = new ExtendedPanel(1.0f);
		northCenterPane.setPreferredSize(new Dimension(0, 4));
		northPane.add(BorderLayout.CENTER, northCenterPane);

		northEastPane = new ExtendedPanel(1.0f);
		northEastPane.setPreferredSize(new Dimension(4, 4));
		northPane.add(BorderLayout.EAST, northEastPane);

		westPane = new ExtendedPanel(1.0f);
		westPane.setPreferredSize(new Dimension(4, 0));
		this.add(BorderLayout.WEST, westPane);

		centerPane = new ExtendedPanel(1.0f);
		centerPane.setLayout(new BorderLayout());
		this.add(BorderLayout.CENTER, centerPane);

		eastPane = new ExtendedPanel(1.0f);
		eastPane.setPreferredSize(new Dimension(4, 0));
		this.add(BorderLayout.EAST, eastPane);

		textLabel = new JLabel();
		textLabel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 4));
		textLabel.setForeground(mouseOutColor);
		centerPane.add(textLabel);

		popupMenu = new JDialog();
		popupMenu.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				popupMenu.setVisible(false);
				textLabel.setForeground(mouseOutColor);
			}
		});
		popupMenu.setUndecorated(true);
	}

	/**
	 * @return the northCenterPane
	 */
	public ExtendedPanel getNorthCenterPane() {
		return northCenterPane;
	}

	/**
	 * @return the northWestPane
	 */
	public ExtendedPanel getNorthWestPane() {
		return northWestPane;
	}

	/**
	 * @return the northEastPane
	 */
	public ExtendedPanel getNorthEastPane() {
		return northEastPane;
	}

	/**
	 * @return the westPane
	 */
	public ExtendedPanel getWestPane() {
		return westPane;
	}

	/**
	 * @return the eastPane
	 */
	public ExtendedPanel getEastPane() {
		return eastPane;
	}

	/**
	 * @return the centerPane
	 */
	public ExtendedPanel getCenterPane() {
		return centerPane;
	}

	private void showMenu(Point pos) {
		if (menuItems.size() == 0)
			return;

		popupMenu.setSize(0, 0);
		popupMenu.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		popupMenu.setLocation(pos);

		for (MenuItem item : menuItems) {
			item.setPreferredSize(new Dimension(maximumWidth, itemHeight));
			if (item.isVisible()) {
				popupMenu.setSize(maximumWidth, popupMenu.getSize().height + itemHeight);

				item.addMouseListener(new ExtendedMouseAdapter() {
					@Override
					public void execute() {
						popupMenu.setVisible(false);
					}
				});
				popupMenu.getContentPane().add(item);

			}
		}

		popupMenu.setVisible(true);
	}
}
